<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>提取一行作为数组</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.pg-fetch-all.html">« pg_fetch_all</a></li>
      <li style="float: right;"><a href="function.pg-fetch-assoc.html">pg_fetch_assoc »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.pgsql.html">PostgreSQL 函数</a></li>
    <li>提取一行作为数组</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.pg-fetch-array" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">pg_fetch_array</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">pg_fetch_array</span> &mdash; <span class="dc-title">提取一行作为数组</span></p>

   </div>
   <div class="refsect1 unknown-unknown-seealsq" id="refsect1-function.pg-fetch-array-unknown-unknown-seealsq">
    <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="methodname"><strong>pg_fetch_array</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$result</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$row</code><span class="initializer"> = ?</span></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$result_type</code><span class="initializer"> = ?</span></span>): <span class="type">array</span></div>

    <p class="para rdfs-comment">
     <span class="function"><strong>pg_fetch_array()</strong></span>
     返回一个与所提取的行（元组/记录）相一致的数组。如果没有更多行可供提取，则返回
     <strong><code>false</code></strong>。
    </p>
    <p class="para">
     <span class="function"><strong>pg_fetch_array()</strong></span> 是 <span class="function"><a href="function.pg-fetch-row.html" class="function">pg_fetch_row()</a></span>
     的扩展版本。在返回的数组中不仅以数字索引方式存放数据（字段编号），默认情况下还用字段名做索引存放数据（字段名）。
    </p>
    <p class="para">
     <code class="parameter">row</code> 是想要取得的行（记录）的编号。第一行为 0。
    </p>
    <p class="para">
     <code class="parameter">result_type</code> 是可选参数，控制着怎样初始化返回值。<code class="parameter">result_type</code>
     是一个常量，可以有以下取值：<strong><code>PGSQL_ASSOC</code></strong>，<strong><code>PGSQL_NUM</code></strong>
     和 <strong><code>PGSQL_BOTH</code></strong>。取值为 <strong><code>PGSQL_ASSOC</code></strong> 时
     <span class="function"><strong>pg_fetch_array()</strong></span> 返回用字段名作为键值索引的关联数组，取值为
     <strong><code>PGSQL_NUM</code></strong> 时用字段编号作为键值，取值为
     <strong><code>PGSQL_BOTH</code></strong> 时则同时用两者作为键值。默认值是 <strong><code>PGSQL_BOTH</code></strong>。
     <blockquote class="note"><p><strong class="note">注意</strong>: 
      <p class="para">
       <code class="parameter">result_type</code> 是在 PHP 4.0 中才加入的参数。
      </p>
     </p></blockquote>
    </p>
    <p class="para">
     <span class="function"><strong>pg_fetch_array()</strong></span> 并不明显比使用 <span class="function"><a href="function.pg-fetch-row.html" class="function">pg_fetch_row()</a></span> 慢，而且在使用中提供了更大的方便。
    </p>
    <p class="para">
     <div class="example" id="example-1904">
      <p><strong>示例 #1 <span class="function"><strong>pg_fetch_array()</strong></span></strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php&nbsp;<br /><br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_pconnect</span><span style="color: #007700">(</span><span style="color: #DD0000">"dbname=publisher"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"An&nbsp;error&nbsp;occured.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;*&nbsp;FROM&nbsp;authors"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"An&nbsp;error&nbsp;occured.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /><br /></span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PGSQL_NUM</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;&lt;-&nbsp;array\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PGSQL_ASSOC</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">"author"</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;&lt;-&nbsp;array\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <p class="para">
      从 4.1.0 开始，<code class="parameter">row</code> 成为可选参数。每次调用 <span class="function"><strong>pg_fetch_array()</strong></span>，内部的行计数器都会加一。
     </p>
    </p></blockquote>
    <p class="para">
     参见 <span class="function"><a href="function.pg-fetch-row.html" class="function">pg_fetch_row()</a></span> 和 <span class="function"><a href="function.pg-fetch-object.html" class="function">pg_fetch_object()</a></span> 以及 <span class="function"><a href="function.pg-fetch-result.html" class="function">pg_fetch_result()</a></span>。
    </p>
   </div>

  </div></div></div></body></html>